package com.gxa.ssm340.controller;

import com.gxa.ssm340.anno.RequiresPermission;
import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.dto.ManagerLoginDTO;
import com.gxa.ssm340.entity.pojo.Get;
import com.gxa.ssm340.entity.pojo.Manager;
import com.gxa.ssm340.entity.vo.UserCouponVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.GetMapper;
import com.gxa.ssm340.service.CpnCancelService;
import com.gxa.ssm340.service.GetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Lenovo
 */
@Api(value = "CpnCancelController",tags = "优惠券核销接口")
@RestController
@RequestMapping("/mgr")
public class CpnCancelController {

    @Autowired
    private CpnCancelService cpnCancelService;

    @Autowired
    private GetService getService;




    @ApiOperation("工作人员登录操作方法")
    @PostMapping("/login")
    public R login( ManagerLoginDTO dto) throws BizException {
        Manager login = cpnCancelService.login(dto);
        return R.success(login);
    }

    @RequiresPermission("mgr:getCpnList")
    @ApiOperation("优惠券查询操作方法")
    @PostMapping("/getCpnList")
    public R getCpnList(String phone,Integer page){
        UserCouponVO[] userCouponVOS = cpnCancelService.selectAllByPhone(phone);
        return R.success(userCouponVOS);
    }

    @RequiresPermission("mgr:useCpn")
    @ApiOperation("核销优惠券操作方法")
    @PostMapping("/useCpn")
    public  R useCpn(String phone,Integer cpId){
        UserCouponVO[] userCouponVOS = cpnCancelService.selectAllByPhone(phone);


        Get get = new Get();
        Get get1 = new Get();
        for (UserCouponVO userCouponVO : userCouponVOS) {
            get.setUid(userCouponVO.getUserId());
            get.setPhone(userCouponVO.getPhone());
            get.setCid(userCouponVO.getId());
            get.setGetIsUsed(userCouponVO.getIsUse());
            get.setGetTime(userCouponVO.getCollectExp());
            get1.setCid(cpId);
            get1.setGetIsUsed(1);
            get1.setUid(userCouponVO.getUserId());
            Integer integer = cpnCancelService.deleteById(get1);
            if(StringUtils.isEmpty(userCouponVOS) || integer < 0 ){
                return R.failed();
            }
        }
        get.setGetUseTime(new Date());

        getService.update(get);
        return R.success();
    }

}
